package q1.project.api.admin.controller.system.role;

// TODO: 替换为自定义注解
// import q1.project.web.annotation.RequireAuth;
// import q1.project.web.annotation.RequirePermission;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import q1.project.core.base.BaseController;
import q1.project.core.result.Result;
import q1.project.log.OperationLog;
import q1.project.modules.system.role.entity.PermissionEntity;
import q1.project.modules.system.role.model.PermissionVm;
import q1.project.modules.system.role.service.PermissionService;

/**
 * 权限管理控制器
 *
 * @author Q1 Team
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/admin/sys/permissions")
@RequiredArgsConstructor
// TODO: @RequireAuth
public class AdminPermissionController extends BaseController {

  private final PermissionService permissionService;

  /** 创建权限 */
  @PostMapping("/create")
  // TODO: @RequirePermission("permission:create")
  @OperationLog(module = "权限管理", description = "创建权限")
  public Result<PermissionVm.PermissionResponse> createPermission(
      @RequestBody PermissionEntity permission) {
    log.info("创建权限请求: permissionCode={}", permission.getPermissionCode());
    PermissionVm.PermissionResponse createdPermission =
        permissionService.createPermission(permission);
    return Result.success(createdPermission);
  }

  /** 更新权限 */
  @PostMapping("/update/{id}")
  // TODO: @RequirePermission("permission:update")
  @OperationLog(module = "权限管理", description = "更新权限")
  public Result<PermissionVm.PermissionResponse> updatePermission(
      @PathVariable String id, @RequestBody PermissionEntity permission) {
    log.info("更新权限请求: id={}", id);
    permission.setId(id);
    PermissionVm.PermissionResponse updatedPermission =
        permissionService.updatePermission(permission);
    return Result.success(updatedPermission);
  }

  /** 删除权限 */
  @PostMapping("/delete/{id}")
  // TODO: @RequirePermission("permission:delete")
  @OperationLog(module = "权限管理", description = "删除权限")
  public Result<Void> deletePermission(@PathVariable String id) {
    log.info("删除权限请求: id={}", id);
    permissionService.deletePermission(id);
    return Result.success();
  }

  /** 查询权限详情 */
  @GetMapping("/{id}")
  // TODO: @RequirePermission("permission:view")
  public Result<PermissionVm.PermissionResponse> getPermissionById(@PathVariable String id) {
    log.info("查询权限详情: id={}", id);
    PermissionVm.PermissionResponse permission = permissionService.getPermissionById(id);
    return Result.success(permission);
  }

  /** 分页查询权限列表 */
  @GetMapping
  // TODO: @RequirePermission("permission:list")
  public Result<Page<PermissionVm.PermissionResponse>> listPermissions(
      PermissionVm.PermissionQuery query) {
    query = handlePageQuery(query);
    logQuery("分页查询权限", query);
    Page<PermissionVm.PermissionResponse> permissions = permissionService.listPermissions(query);
    return Result.success(permissions);
  }

  /** 根据模块查询权限列表 */
  @GetMapping("/module/{module}")
  // TODO: @RequirePermission("permission:list")
  public Result<List<PermissionVm.PermissionResponse>> listPermissionsByModule(
      @PathVariable String module) {
    log.info("根据模块查询权限: module={}", module);
    List<PermissionVm.PermissionResponse> permissions =
        permissionService.listPermissionsByModule(module);
    return Result.success(permissions);
  }

  /** 查询所有启用的权限列表 */
  @GetMapping("/enabled")
  public Result<List<PermissionVm.PermissionResponse>> listEnabledPermissions() {
    log.info("查询所有启用的权限");
    List<PermissionVm.PermissionResponse> permissions = permissionService.listEnabledPermissions();
    return Result.success(permissions);
  }
}
